﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.NamHocService;
namespace eTMS.Controllers{
public class NamHocControllerBase : CommonController{
	public NamHocControllerBase()
		: base()
	{	
		this.dao = new NamHocServiceClient();
	}
	~NamHocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private NamHocEntity oNamHoc;	
	public NamHocEntity NamHocObject{
		get{
			return oNamHoc;
		}
		set{
			oNamHoc=value;
		}
	}
	protected NamHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSNamHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSNamHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oNamHoc==null)return false;
		bool result=true;
		try{
			oNamHoc = dao.SaveRet(oNamHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListNamHoc.Add(oNamHoc);
		}
		return result;
	}
	public bool Update(){
		if(oNamHoc==null)return false;
		return dao.Update(oNamHoc);
	}
	public bool Delete(){
		if(oNamHoc==null)return false;
		bool retvalue = dao.Delete(oNamHoc);
		if(retvalue){			
			_ListNamHoc.Remove(oNamHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 NamHoc_ID){
		bool retvalue = dao.DeleteByID(NamHoc_ID);		
		if(retvalue){
			NamHocEntity oDeleted=FindNamHoc(NamHoc_ID);
			if(oDeleted!=null){
				_ListNamHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oNamHoc = new NamHocEntity();
	}
	//Check if exists MaNamHoc
	public bool CheckExistsMaNamHoc(object value){	
		return dao.SelectOneByMaNamHoc(ConvertInt(value)).MaNamHoc!=0;
    }    
    //Copy data from data row in grid to object
    public NamHocEntity MapDataRowToObject(DataRow row){
		NamHocEntity o = new NamHocEntity();
		 if(!row.IsNull("MaNamHoc"))
			o.MaNamHoc=ConvertInt(row["MaNamHoc"]);
		 o.TenNamHoc=Convert.ToString(row["TenNamHoc"]);
		 if(!row.IsNull("NgayBatDau"))
			o.NgayBatDau=ConvertDateTime(row["NgayBatDau"]);
		 if(!row.IsNull("NgayKetThuc"))
			o.NgayKetThuc=ConvertDateTime(row["NgayKetThuc"]);
		 if(!row.IsNull("NamHocHienTai"))
			o.NamHocHienTai=ConvertBool(row["NamHocHienTai"]);
		 if(!row.IsNull("NamHoc_ID"))
		    o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(NamHocEntity o,DataRow row){
		row["MaNamHoc"]=o.MaNamHoc;
		row["TenNamHoc"]=o.TenNamHoc;
		row["NgayBatDau"]=o.NgayBatDau;
		row["NgayKetThuc"]=o.NgayKetThuc;
		row["NamHocHienTai"]=o.NamHocHienTai;
		row["NamHoc_ID"]=o.NamHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.NamHocObject,row);
    }
	public void LoadObject(Int32 NamHoc_ID){
		NamHocEntity oLoad=FindNamHoc(NamHoc_ID);
		if(oLoad==null){
			oNamHoc=this.LoadNamHoc(NamHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oNamHoc.IsNullNamHoc_ID;
			if(!bNotIsNull){
				this.ListNamHoc.Add(oNamHoc);
			}
		}
		else{
			oNamHoc=oLoad;
		}
	}
	#region CachedList NamHoc
	private static List<NamHocEntity> _ListNamHoc=new List<NamHocEntity>();
    public List<NamHocEntity>ListNamHoc{
		get{
			return _ListNamHoc;
		}
	}
	public static List<NamHocEntity>ListNamHocStatic{
		get{
			return _ListNamHoc;
		}
	}
	public NamHocEntity FindNamHoc(Int32 NamHoc_ID){
		NamHocEntity oResult=this.ListNamHoc.Find(delegate(NamHocEntity oFind){
			bool result=true; 
			result&=oFind.NamHoc_ID.Equals(NamHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllNamHoc(bool clearCache){
		if(_ListNamHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		NamHocServiceClient dao = new NamHocServiceClient();
		_ListNamHoc=new List<NamHocEntity>(dao.SelectAll());
        _ListNamHoc.Sort(new Comparison<NamHocEntity>(delegate(NamHocEntity o1, NamHocEntity o2) { return o2.NgayBatDau.CompareTo(o1.NgayBatDau); }));
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public NamHocEntity LoadNamHoc(Int32 NamHoc_ID){			
		NamHocEntity o=dao.SelectOne(NamHoc_ID);		
		return o;
	}
	#endregion
}
}
